﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.Common.SysMgr;

namespace Sunimage.Spjg.DAL.SysMgr
{
    public class UserDa
    {
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        public static void Insert(User user)
        {
            Insert(user, DBHelper.GetDatabase(), null);
        }
        public static void Insert(User user, TranscationHelper trans)
        {
            Insert(user, trans.DataBase, trans.Transaction);
        }
        public static void Insert(User user, Database db, DbTransaction trans)
        {
            object[] pars = new object[]
            {
                user.Username,
                user.Password,
                user.Email,
                user.NickName,
                user.LastLogin,
                user.Disabled,
                user.Role
            };
            try
            {
                if (trans == null)
                    user.Id = Convert.ToInt32(db.ExecuteScalar("Usp_InsertUser", pars));
                else
                    user.Id = Convert.ToInt32(db.ExecuteScalar(trans, "Usp_InsertUser", pars));

            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        public static void Delete(int id)
        {
            Delete(id, DBHelper.GetDatabase(), null);
        }
        public static void Delete(int id, TranscationHelper trans)
        {
            Delete(id, trans.DataBase, trans.Transaction);
        }
        public static void Delete(int id, Database db, DbTransaction trans)
        {
            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_DeleteUser", id);
                else
                    db.ExecuteNonQuery(trans, "Usp_DeleteUser", id);

            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, true);
            }
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="user"></param>
        public static void Update(User user)
        {
            Update(user, DBHelper.GetDatabase(), null);
        }
        public static void Update(User user, TranscationHelper trans)
        {
            Update(user, trans.DataBase, trans.Transaction);
        }
        public static void Update(User user, Database db, DbTransaction trans)
        {
            object[] pars = new object[]
            {
                user.Id,
                user.Username,
                user.Password,
                user.Email,
                user.NickName,
                user.LastLogin,
                user.Disabled,
                user.Role
            };

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery("Usp_UpdateUser", pars);
                else
                    db.ExecuteNonQuery(trans, "Usp_UpdateUser", pars);

            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        /// <summary>
        /// 获取所有数据用户信息
        /// </summary>
        /// <returns></returns>
        public static List<User> GetAll()
        {
            return GetAll(DBHelper.GetDatabase(), null);

        }
        public static List<User> GetAll(TranscationHelper trans)
        {
            return GetAll(trans.DataBase, trans.Transaction);
        }
        public static List<User> GetAll(Database db, DbTransaction trans)
        {
            List<User> gsjUsers = new List<User>();
            IDataReader dataReader = null;
            if (trans == null)
                dataReader = db.ExecuteReader("Usp_GetUserList");
            else
                dataReader = db.ExecuteReader(trans, "Usp_GetUserList");

            using (dataReader)
            {
                while (dataReader.Read())
                {
                    gsjUsers.Add(PopulateUser(dataReader));
                }
            }

            return gsjUsers;
        }

        //辅助方法
        private static User PopulateUser(IDataReader dataReader)
        {
            User gsjUser = new User();
            gsjUser.Id = dataReader.GetInt32(0);
            gsjUser.Username = dataReader.GetString(1);
            gsjUser.Password = dataReader.GetString(2);
            if (!dataReader.IsDBNull(3))
                gsjUser.Email = dataReader.GetString(3);
            if (!dataReader.IsDBNull(4))
                gsjUser.NickName = dataReader.GetString(4);
            if (!dataReader.IsDBNull(5))
                gsjUser.LastLogin = dataReader.GetDateTime(5);
            gsjUser.Disabled = dataReader.GetBoolean(6);
            gsjUser.Role = (RoleType)dataReader.GetInt32(7);
            return gsjUser;
        }
    }
}
